/*
* Copyright (C) 2008 Google Enterprise EMEA
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.gsa.aasr;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.Collection;
import java.util.Hashtable;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.log4j.Logger;


/**
 * AASRServiceManager manages the hash table of services. It loads the service
 * descriptions from the XML configuration file "aaservices.xml"
 */
public class

AASRServiceManager {
    private Hashtable services = null;
    private static org.apache.log4j.Logger log4jLogger = null;

    public AASRServiceManager() {
        log4jLogger = Logger.getLogger(AASRServiceManager.class);
    }

    public void loadServices() throws MalformedURLException, 
                                      ConfigurationException, 
                                      InstantiationException, 
                                      IllegalAccessException, 
                                      ClassNotFoundException {
        XMLConfiguration config = null;
        services = new Hashtable();
        config = new XMLConfiguration("aasrservices.xml");
        log4jLogger.debug("aasrservices.xml file loaded");
        Object prop = config.getProperty("service[@name]");

        if (prop instanceof Collection) {
            int colSize = ((Collection)prop).size();
            for (int i = 0; i < colSize; i++) {
                AASRService s;
                String className = 
                    config.getProperty("service(" + i + ")[@class]").toString();
                    
                s = (AASRService)Class.forName(className).newInstance();
                
                String serviceName = 
                    config.getProperty("service(" + i + ")[@name]").toString();
                s.setName(serviceName);
                s.setUser(config.getProperty("service(" + i + 
                                             ").login.username").toString());
                s.setPasswd(config.getProperty("service(" + i + 
                                               ").login.password").toString());
                s.setDateformat(config.getProperty("service(" + i + 
                                                   ").dateformat").toString());
                URL url = null;
                url = 
                        new URL(config.getProperty("service(" + i + ").url").toString());

                s.setUrl(url);
                s.setAdditional(config.getProperty("service(" + i + 
                                                   ").additional").toString());       
                s.init();
                services.put(serviceName, s);
                log4jLogger.debug("Service "  + serviceName + " loaded");
            }
        } else {
            AASRService s;
            String className = 
                config.getProperty("service@[class]").toString();
                
            s = (AASRService)Class.forName(className).newInstance();
            
            String serviceName = config.getProperty("service@[name]").toString();
            s.setName(serviceName);
            s.setUser(config.getProperty("service.login.username").toString());
            s.setPasswd(config.getProperty("service.login.password").toString());
            s.setDateformat(config.getProperty("service.dateformat").toString());
            URL url = null;
            url = 
                    new URL(config.getProperty("service.url").toString());
            s.setUrl(url);
            s.setAdditional(config.getProperty("service.additional").toString());
            s.init();
            services.put(serviceName, s);
            log4jLogger.debug("Service "  + serviceName + " loaded");
        }
    }

    public AASRService getService(String s) {
        AASRService serv = null;
        serv = (AASRService)(services.get(s));
        return serv;
    }
}
